package ClassWork.Work_03.Cart;

import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class CartService {
    public static void cartService(List<Produce> pro, Map<String,Cart> map){
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入购买商品的编号：");
        int n = sc.nextInt();
        Produce produce = pro.get(n-1);
        System.out.print("请输入要购买的数量：");
        int m = sc.nextInt();
        Cart cart = map.get(produce.getPNum());
        if(cart == null){
            cart = new Cart(m,produce);
        }else{
            int t = cart.getNum();
            cart.setNum(m + t);
        }
        map.put(produce.getPNum(),cart);
    }

    public static void putCart(Map<String,Cart> map){
        Set<String> keys = map.keySet();
        int i = 1;
        double sum = 0;
        for(String PNum : keys){
            Cart cart = map.get(PNum);
            Produce produce = cart.getPro();
            System.out.println(i + "  " + produce.getPName() + "  " + produce.getPrice()
                    + "  " + cart.getNum() + "  " + cart.getNum() * produce.getPrice());
            sum += cart.getNum() * produce.getPrice();
            i++;
        }
        System.out.println("总计：" + sum);
    }
}
